package cn.yitun.chat.yitunserverimcore.general.main.push.group;

import cn.yitun.chat.yituncommonmessage.push.PushMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.enums.control.push.group.GroupBusinessPushMethodEnum;
import cn.yitun.chat.yitunserverimcore.bridge.net.WriteHandler;
import cn.yitun.chat.yitunserverimcore.general.common.util.PushMessageBuild;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 群业务相关推送
 * Date 2019-01-27 21:43:21<br>
 * Description
 * 
 * @author yitun<br>
 * @since 1.1
 */
@Service
public class GroupBusinessPush {
	//1.2.201
	final String action = ActionTypeConstant.GROUP_BUSINESS_ACTION_PUSH;

	@Resource
	WriteHandler writeHandler;

	/**
	 * 更改群主信息
	 * @param key
	 * @param groupId
	 * @param oldOwnerUserId
	 * @param newOwnerUserId
	 * @param userIdList
	 */
	public void pushChangeGroupOwner(Long key, Long groupId, Long oldOwnerUserId, Long newOwnerUserId, List<Long> userIdList) {
		String method = GroupBusinessPushMethodEnum.CHANGE_GROUP_OWNER.getMethod();
		PushMessage message = PushMessageBuild.build(key, action, method);
		message.bodyPut("groupId", groupId);
		message.bodyPut("oldOwnerUserId", oldOwnerUserId);
		message.bodyPut("newOwnerUserId", newOwnerUserId);
		writeHandler.push(YitunStringUtil.toStrList(userIdList), message);
	}

	/**
	 * 解除
	 * @param key
	 * @param groupId
	 * @param ownerUserId
	 * @param userId
	 */
	public void pushDisband(Long key, Long groupId, Long ownerUserId, Long userId) {
		List<Long> userIdList = new ArrayList<>();
		userIdList.add(userId);
		pushDisband(key, groupId, ownerUserId, userIdList);
	}

	/**
	 * 解散
	 * @param key
	 * @param groupId
	 * @param ownerUserId
	 * @param userIdList
	 */
	public void pushDisband(Long key, Long groupId, Long ownerUserId, List<Long> userIdList) {
		String method = GroupBusinessPushMethodEnum.DISBAND.getMethod();
		PushMessage message = PushMessageBuild.build(key, action, method);
		message.bodyPut("groupId", groupId);
		message.bodyPut("ownerUserId", ownerUserId);
		writeHandler.push(YitunStringUtil.toStrList(userIdList), message);
	}

	/**
	 * 放弃
	 * @param key
	 * @param groupId
	 * @param userId
	 * @param userIdList
	 */
	public void pushQuit(Long key, Long groupId, Long userId, List<Long> userIdList) {
		String method = GroupBusinessPushMethodEnum.QUIT.getMethod();
		PushMessage message = PushMessageBuild.build(key, action, method);
		message.bodyPut("groupId", groupId);
		message.bodyPut("userId", userId);
		writeHandler.push(YitunStringUtil.toStrList(userIdList), message);
	}
}
